Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 481690a3b6
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| (ai-code-mcp-debug-tools--register-base-tools) | ||
| (when enable-eval-elisp | ||
| (ai-code-mcp-debug-tools--register-eval-tool)) |
There was a problem hiding this comment.
Keep session-only debug tools out of global MCP tool list
ai-code-mcp-debug-tools-enable-for-session is documented as session-scoped, but this code path unconditionally registers debug tools into the global ai-code-mcp-server-tools registry. Since ai-code-mcp--tools-list returns that global list for every session, enabling debug tools for one session makes other sessions advertise those tools even when they are disabled there, which leads agents to call tools that will fail at runtime and breaks the session-scoping contract.
Useful? React with 👍 / 👎.
| (when (and session-id | ||
| (fboundp 'ai-code-mcp-debug-tools-enable-for-session)) |
There was a problem hiding this comment.
Fail fast when no MCP session is attached for runtime debug
This command still builds and sends a prompt asserting that Emacs MCP tools are available even when no session ID exists (for example, non-MCP backends or no active session), because tool enablement is conditional later and silently skipped. In that state the AI receives inaccurate capabilities and will attempt tool calls that cannot work, so the flow should error early or generate a non-MCP prompt variant.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Pull request overview
Adds a dedicated “Emacs runtime debugging” flow to the package, allowing users to compose a focused debugging prompt for the active AI session and enable MCP debug tools on a per-session basis (optionally including eval_elisp), with ERT coverage for the new behavior.
Changes:
- Added
ai-code-debug-emacs-runtimeand wired it into the “Other Tools” transient menu. - Introduced session-scoped enabling for MCP debug tools (and optional
eval_elisp) via session overrides. - Added/updated ERT tests covering the new menu entry, prompt flow, and session override behavior.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
ai-code.el |
Adds the runtime debug command, active MCP session-id lookup, and a new transient menu entry. |
ai-code-mcp-debug-tools.el |
Adds session override state + enable-for-session entrypoint; gates debug tool functions on per-session enablement. |
test/test_ai-code.el |
Adds tests for the new runtime debug prompt flow and transient menu entry. |
test/test_ai-code-mcp-debug-tools.el |
Adds test ensuring session overrides can enable tools even when globally disabled. |
| (defvar ai-code-mcp-agent--session-id nil | ||
| "Buffer-local MCP session id attached by `ai-code-mcp-agent`.") |
| (defun ai-code-debug-emacs-runtime () | ||
| "Assemble and send an Emacs runtime debugging prompt for the current AI session." | ||
| (interactive) | ||
| (let* ((description | ||
| (ai-code-read-string | ||
| "Describe the Emacs runtime issue (eg: key binding / interactive function): ")) | ||
| (enable-eval-elisp | ||
| (y-or-n-p | ||
| "Allow AI to eval Emacs Lisp while debugging this Emacs runtime issue? ")) | ||
| (session-id (ai-code--active-mcp-session-id))) | ||
| (when description | ||
| (when-let* ((prompt | ||
| (ai-code-read-string | ||
| "Confirm and edit Emacs runtime debug prompt: " | ||
| (ai-code--emacs-runtime-debug-prompt | ||
| description | ||
| enable-eval-elisp)))) | ||
| (when (and session-id | ||
| (fboundp 'ai-code-mcp-debug-tools-enable-for-session)) | ||
| (ai-code-mcp-debug-tools-enable-for-session | ||
| session-id | ||
| enable-eval-elisp)) | ||
| (ai-code--insert-prompt prompt))))) |
| ("p" "Open prompt history file" ai-code-open-prompt-file) | ||
| ("m" "Debug python MCP server" ai-code-debug-mcp) | ||
| ("N" "Toggle notifications" ai-code-notifications-toggle) | ||
| ;; DONE: add a menu item: Debug your emacs runtime. It will temporarily enable ai-code-mcp-debug-tools-enabled, and ask user if they want to enable ai-code-mcp-debug-tools-enable-eval-elisp (eval elisp with AI?) to further help debugging. User can describe what happens (We prompt them that it can debug an interactive function or a key-binding). The final prompt will assemble with user description and then tell AI to user emacs mcp tools to debug. After user confirm the prompt, send to AI. |
| (defvar ai-code-mcp--current-session-id nil | ||
| "Dynamically bound MCP session id for the current tool invocation.") |
| (puthash session-id | ||
| `((enabled . t) | ||
| (enable_eval_elisp . ,(and enable-eval-elisp t))) | ||
| ai-code-mcp-debug-tools--session-overrides) | ||
| (ai-code-mcp--ensure-error-capture) | ||
| (ai-code-mcp-debug-tools--register-base-tools) | ||
| (when enable-eval-elisp | ||
| (ai-code-mcp-debug-tools--register-eval-tool)) |
| (when enable-eval-elisp | ||
| (ai-code-mcp-debug-tools--register-eval-tool)) | ||
| session-id) | ||
|
|
There was a problem hiding this comment.
Pull request overview
Adds an Emacs runtime debugging flow that helps users assemble a focused “debug my Emacs runtime” prompt for the active AI session, while ensuring MCP debug tool access is governed strictly by the existing global debug flags.
Changes:
- Add
ai-code-debug-emacs-runtimeand a helper prompt builder to generate a runtime-debugging prompt from user input. - Remove/avoid session-scoped overrides by enforcing global gating for debug tools (including
eval_elisp). - Expand ERT coverage for the runtime-debug prompt text, menu entry wiring, and global-disable error paths.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
ai-code.el |
Adds runtime-debug prompt builder + interactive command, and wires the command into the “Other Tools” transient menu. |
ai-code-mcp-debug-tools.el |
Refactors tool registration and adds explicit global gating checks to debug tool entry points. |
test/test_ai-code.el |
Adds tests for runtime-debug prompt composition and transient menu entry presence. |
test/test_ai-code-mcp-debug-tools.el |
Adds coverage ensuring globally disabled debug tools block direct calls. |
| ("p" "Open prompt history file" ai-code-open-prompt-file) | ||
| ("m" "Debug python MCP server" ai-code-debug-mcp) | ||
| ("N" "Toggle notifications" ai-code-notifications-toggle) | ||
| ;; DONE: add a menu item: Debug your emacs runtime. It will temporarily enable ai-code-mcp-debug-tools-enabled, and ask user if they want to enable ai-code-mcp-debug-tools-enable-eval-elisp (eval elisp with AI?) to further help debugging. User can describe what happens (We prompt them that it can debug an interactive function or a key-binding). The final prompt will assemble with user description and then tell AI to user emacs mcp tools to debug. After user confirm the prompt, send to AI. |
| (error "Emacs debug MCP tools are disabled for this session"))) | ||
|
|
||
| (defun ai-code-mcp-debug-tools--require-eval-enabled () | ||
| "Signal an error unless `eval_elisp' is enabled." | ||
| (unless (ai-code-mcp-debug-tools--eval-enabled-p) | ||
| (error "The eval_elisp tool is disabled for this session"))) |
| (defun ai-code--emacs-runtime-debug-prompt (description enable-eval-elisp) | ||
| "Return an Emacs runtime debugging prompt from DESCRIPTION. | ||
| ENABLE-EVAL-ELISP describes whether `eval_elisp' is available." | ||
| (format | ||
| (concat | ||
| "Use the Emacs MCP tools available in this session to debug my Emacs runtime.\n" | ||
| "The issue may involve an interactive function or a key binding.\n" | ||
| "%s\n\n" | ||
| "Inspect the relevant runtime state first: keymaps, command metadata,\n" | ||
| "variables, recent messages, load state, and the last backtrace when useful.\n" | ||
| "Explain what you find, then recommend the smallest fix or next step.\n\n" | ||
| "Runtime issue description:\n" | ||
| "%s") | ||
| (if enable-eval-elisp | ||
| "eval_elisp is enabled in your Emacs MCP config." | ||
| "eval_elisp is disabled in your Emacs MCP config, so rely on non-eval inspection tools unless you first enable ai-code-mcp-debug-tools-enable-eval-elisp.") | ||
| description)) |
Summary
Add an Emacs runtime debugging flow that builds a focused prompt for the active AI session, while keeping MCP access tied to the existing global debug flags.
This is meant for cases like a broken key binding, an interactive command that behaves unexpectedly, or package/config runtime issues where the AI should inspect Emacs state with the MCP debug tools.
What changed
ai-code-debug-emacs-runtimeentry in the Other Tools menu and let it assemble a runtime-debugging prompt around the user's descriptioneval_elispai-code-mcp-debug-tools-enable-eval-elispis offVerification
emacs -Q -batch -L . -L test/stubs -l test/test_00-bootstrap.el -l ert -l test/test_ai-code.el -f ert-run-tests-batch-and-exitemacs -Q -batch -L . -L test/stubs -l test/test_00-bootstrap.el -l ert -l test/test_ai-code-mcp-debug-tools.el -f ert-run-tests-batch-and-exitemacs -Q -batch -L . -L test/stubs -l test/test_00-bootstrap.el -f batch-byte-compile ai-code.el ai-code-mcp-debug-tools.el test/test_ai-code.el test/test_ai-code-mcp-debug-tools.elemacs -Q -batch -L . -L test/stubs -l test/test_00-bootstrap.el --eval '(progn (require '\''checkdoc) (checkdoc-file "ai-code.el") (checkdoc-file "ai-code-mcp-debug-tools.el") (checkdoc-file "test/test_ai-code.el") (checkdoc-file "test/test_ai-code-mcp-debug-tools.el"))'